package com.fingard.dsp.bank.directbank.ceb02.bankUtil;

import com.fingard.FGBiz;

import java.io.*;
import java.util.TimeZone;

public class ReadContentToTxt {
	
	public final static String  filepath = FGBiz.DSPHOME_Parent+File.separator + "cebkey" +File.separator+"mac.txt";

	public static String getString() {
		FileInputStream in = null;
		// 读取当前文件的更新时间
		File file = new File(filepath);
		if(!file.exists()) {
			try {
				File dir =new File(filepath.substring(0,filepath.length()-8));
				if(!dir.exists()) {
					dir.mkdir();
				}
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String encoding = "UTF-8";
		long time = file.lastModified();
		long current = System.currentTimeMillis();
		long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
		if (time < zero) {
			return null;
		}
		Long filelength = file.length();
		byte[] filecontent = new byte[filelength.intValue()];
		try {
			in = new FileInputStream(file);
			in.read(filecontent);
			in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			return new String(filecontent, encoding);
		} catch (UnsupportedEncodingException e) {
			System.err.println("The txt does not support " + encoding);
			e.printStackTrace();
			return null;
		} finally {
            try {
                if(in != null){
                	in.close();
                }            
            } catch (Exception e2) {
                e2.printStackTrace();
            }
	    }
	}
	
	private static File File(String substring) {
		// TODO Auto-generated method stub
		return null;
	}

	public static void setString(String[] args) {
		PrintStream ps = null;
        try {      	
        	File file = new File(filepath);
            if(file.exists()) {
            	file.delete();
            }
            try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            ps = new PrintStream(new FileOutputStream(file));
            ps.println(args[0]+","+args[1]);// 往文件里写入字符串
            ps.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ps != null){
                	ps.close();
                }            
            } catch (Exception e2) {
                e2.printStackTrace();
            }
	}
        }
}